package com.yc.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.cloud.admin.dto.request.AlarmRecordUploadReqDTO.DetectionPositionDTO;
import com.yc.cloud.admin.service.DetectionPositionService;
import com.yc.cloud.admin.vo.DetectionPositionVo;
import com.yc.cloud.entity.DetectionPosition;
import com.yc.cloud.mapper.DetectionPositionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 目标检测位置信息表 服务实现类
 * </p>
 *
 * @author kinggu
 * @since 2025-07-22
 */
@Service
public class DetectionPositionServiceImpl extends ServiceImpl<DetectionPositionMapper, DetectionPosition> implements DetectionPositionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(Long alarmRecordId, List<DetectionPositionDTO> detectionPositionDTOList) {
        if (detectionPositionDTOList == null || detectionPositionDTOList.isEmpty()) {
            return true;
        }

        List<DetectionPosition> detectionPositionList = detectionPositionDTOList.stream()
                .map(dto -> {
                    DetectionPosition detectionPosition = new DetectionPosition();
                    detectionPosition.setAlarmRecordId(alarmRecordId);
                    detectionPosition.setTargetType(dto.getTargetType());
                    detectionPosition.setConfidence(dto.getConfidence());
                    detectionPosition.setX(dto.getX());
                    detectionPosition.setY(dto.getY());
                    detectionPosition.setCreateBy(0L);
                    detectionPosition.setUpdateBy(0L);
                    detectionPosition.setWidth(dto.getWidth());
                    detectionPosition.setHeight(dto.getHeight());
                    return detectionPosition;
                })
                .collect(Collectors.toList());

        return this.saveBatch(detectionPositionList);
    }

    @Override
    public List<DetectionPositionVo> listByAlarmRecordId(Long alarmRecordId) {
        LambdaQueryWrapper<DetectionPosition> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DetectionPosition::getAlarmRecordId, alarmRecordId);
        queryWrapper.eq(DetectionPosition::getDeleted, false);

        List<DetectionPosition> detectionPositionList = this.list(queryWrapper);
        return convertToVoList(detectionPositionList);
    }

    private List<DetectionPositionVo> convertToVoList(List<DetectionPosition> entityList) {
        if (entityList == null || entityList.isEmpty()) {
            return new ArrayList<>();
        }

        return entityList.stream()
                .map(entity -> {
                    DetectionPositionVo vo = new DetectionPositionVo();
                    BeanUtils.copyProperties(entity, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }
} 